package com.hoperun.checkcloth.ui

import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import at.favre.lib.planb.full.CrashExplorerOverviewActivity
import com.blankj.utilcode.util.AppUtils
import com.hoperun.checkcloth.R
import com.hoperun.checkcloth.databinding.HomeActivityBinding
import com.hoperun.checkcloth.view.SimpleDialog
import com.hoperun.checkcloth.viewmodel.HomeViewModel
import com.zhuyu.downloadlibrary.DownloadDialog
import com.zhuyu.downloadlibrary.DownloadDialog.DownloadSimpleListener

class HomeActivity : AppCompatActivity() {
    lateinit var binding: HomeActivityBinding
    lateinit var viewModel: HomeViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProvider(this)[HomeViewModel::class.java]
        binding = DataBindingUtil.setContentView(this, R.layout.home_activity)
        setupViewModel()


        binding.versionlab.setText("版本号："+AppUtils.getAppVersionName());
    }
//    private String getVersion() {
//        try {
//            PackageManager manager = this.getPackageManager();
//            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
//            String version = info.versionName;
//            return version;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "无法获取到版本号";
//        }
//    }
    private fun setupViewModel() {
        binding.viewModel = viewModel

        viewModel.navigation.observe(this, { path ->
            Intent().let {
                it.setClassName(baseContext, path)
                startActivity(it)
            }
        })

        viewModel.upgradeInfo.observe(this, { (force, url, path) ->
            showUpgradeDialog(force, url, path)
        })

        viewModel.startCrashExplore.observe(this, {
            CrashExplorerOverviewActivity.start(this)
        })

        viewModel.init()
    }

    private fun showUpgradeDialog(_force: Boolean, _url: String, _filePath: String) {
        val showDownload: ((Boolean, String, String) -> Unit) = { force, url, filePath ->
            val dowDialog = DownloadDialog.newInstance(url, filePath, force)
            dowDialog.setDownloadListener(object : DownloadSimpleListener {
                override fun onSuccess() {
                    AppUtils.installApp(filePath)
                }

                override fun onError() {
                    Toast.makeText(baseContext, R.string.download_fail, Toast.LENGTH_SHORT).show()
                    SimpleDialog.show(baseContext, resources.getString(R.string.download_fail))
                }
            })
            dowDialog.show(supportFragmentManager, "DownLoad")
        }

        if (_force) {
            SimpleDialog.forceShow(this,
                    resources.getString(R.string.application_has_new_version_need_force_update),
                    SimpleDialog.OnPositiveClickListener {
                        showDownload(_force, _url, _filePath)
                    })
        } else {
            SimpleDialog.show(this,
                    resources.getString(R.string.application_has_new_version_need_update),
                    SimpleDialog.OnPositiveClickListener {
                        showDownload(_force, _url, _filePath)
                    })
        }
    }
}